package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PictureDao;
import com.itheima.dao.PictureSortDao;
import com.itheima.pojo.Picture;
import com.itheima.pojo.PictureSort;
import com.itheima.service.PictureService;
import com.itheima.utils.DateUtils;
import com.itheima.utils.IdWorker;
import com.itheima.vo.PictureVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author kehaonan
 * @version 1.0
 * @date 2020/7/8 9:20
 */
@Service
public class PictureServiceImpl implements PictureService {
    @Autowired
    private PictureDao pictureDao;

    @Autowired
    private PictureSortDao pictureSortDao;

    @Autowired
    public IdWorker idWorker;

    @Override
    public void add(PictureVO pictureVO) {
        //将图片信息保存到数据库
        Picture picture = new Picture();
        picture.setUid(String.valueOf(idWorker.nextId()));
        picture.setFileUid(pictureVO.getFileUid());
        picture.setPicName(pictureVO.getPicName());
        picture.setPictureSortUid(pictureVO.getPictureSortUid());
        //图片状态改为1
        picture.setStatus(1);
        picture.setCreateTime(DateUtils.getNowTime());
        picture.setUpdateTime(DateUtils.getNowTime());
        pictureDao.insert(picture);
    }

    @Override
    public void delete(PictureVO pictureVO) {
        Picture picture = new Picture();
        picture.setUid(pictureVO.getUid());
        //删除做逻辑删除,设置状态为0
        pictureVO.setStatus(0);
        //修改状态就相当于删除
        picture.setUpdateTime(DateUtils.getNowTime());
        pictureDao.updateByPrimaryKeySelective(picture);
    }

    @Override
    public List<Picture> getList(PictureVO pictureVO) {
        //当前页
        Integer currentPage = pictureVO.getCurrentPage();
        //每页数据
        Integer pageSize = pictureVO.getPageSize();
        //分页查询
        PageHelper.startPage(currentPage, pageSize);

        //查询条件构造
        Example example=new Example(Picture.class);
        Example.Criteria criteria = example.createCriteria();
        //状态为1的才可以被查询到
        criteria.andEqualTo("status", 1);

        return (Page<Picture>) pictureDao.selectByExample(example);
    }

    @Override
    public void edit(PictureVO pictureVO) {
        Picture picture = new Picture();
        picture.setUid(pictureVO.getUid());
        picture.setFileUid(pictureVO.getFileUid());
        picture.setPicName(pictureVO.getPicName());
        picture.setPictureSortUid(pictureVO.getPictureSortUid());
        picture.setStatus(pictureVO.getStatus());
        picture.setUpdateTime(DateUtils.getNowTime());

        pictureDao.updateByPrimaryKeySelective(picture);
    }

    @Override
    public void setCover(PictureVO pictureVO) {
        //封面是分类封面
        //就需要设置图片分类的文件id就可以把图片展示到分类里面

        //先根据pictureSortUid查询是否有对应的图片分类信息
        PictureSort pictureSort = new PictureSort();
        pictureSort.setUid(pictureVO.getPictureSortUid());

        pictureSort = pictureSortDao.selectOne(pictureSort);

        if (pictureSort==null){
            throw new RuntimeException("没有对应图片分类UID");
        }

        //有就将图片的id设置为图片分类的fileUid
        pictureSort.setFileUid(pictureVO.getUid());
        pictureSort.setUpdateTime(DateUtils.getNowTime());

        pictureSortDao.updateByPrimaryKeySelective(pictureSort);
    }

    @Override
    public Picture getPictureSortByUid(String pictureId) {
        return pictureDao.selectByPrimaryKey(pictureId);
    }
}
